/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.internal;

import org.rcgwt.client.ui.grid.internal.model.IElementModel;
import org.rcgwt.client.ui.grid.internal.model.IElementModelDelta;
import org.rcgwt.client.ui.grid.internal.model.IGridModel;
import org.rcgwt.client.ui.grid.internal.model.IHeaderModel;
import org.rcgwt.client.ui.grid.internal.model.IViewModel;
import org.rcgwt.client.ui.grid.internal.model.IViewModelElements;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public abstract class AbstractViewModel extends AbstractElementModel implements
		IViewModel {

	private final IGridModel gridModel;

	private IHeaderModel headerModels[];

	private int renderHints;

	private IViewModelElements viewModelElements;

	private int scrollLeft;

	private int scrollTop;

	public AbstractViewModel(int modelIndex, IGridModel gridModel,
			int renderHints) {
		this.gridModel = gridModel;

		this.modelIndex = modelIndex;

		this.renderHints = renderHints;
	}

	protected void setViewModelElements(IViewModelElements viewModelElements) {
		this.viewModelElements = viewModelElements;
	}

	@Override
	public IViewModelElements getViewModelElements() {
		return viewModelElements;
	}

	public void setHeaders(IHeaderModel headerModels[]) {
		this.headerModels = headerModels;
	}

	@Override
	public IHeaderModel getHeaderModelByPosition(IHeaderModel.Position position) {

		for (IHeaderModel headerModel : headerModels) {

			if (headerModel.getPosition() == position) {
				return headerModel;
			}
		}

		return null;
	}

	@Override
	public IGridModel getGridModel() {
		return gridModel;
	}

	@Override
	public IHeaderModel[] listHeaderModels() {
		return headerModels;
	}

	@Override
	public int getRenderHints() {
		return renderHints;
	}

	@Override
	public IElementModel getParent() {
		return getGridModel();
	}

	@Override
	public final Type getType() {
		return Type.VIEW;
	}

	@SuppressWarnings("unchecked")
	public boolean setScrollPositions(int scrollLeft, int scrollTop) {
		if (scrollLeft == this.scrollLeft && scrollTop == this.scrollTop) {
			return false;
		}

		int oldLeft = scrollLeft;
		int oldTop = scrollTop;

		this.scrollLeft = scrollLeft;
		this.scrollTop = scrollTop;

		IElementModelDelta<IViewModel> modelElementDelta = (IElementModelDelta<IViewModel>) getModelElementDelta();

		((ViewModelDelta) modelElementDelta).performScrollPositionsChanged(
				oldLeft, oldTop, scrollLeft, scrollTop);

		return true;
	}

	@Override
	public IElementModelDelta<?> createDelta() {
		return new ViewModelDelta(this);
	}

	@Override
	public int getScrollLeft() {
		return scrollLeft;
	}

	@Override
	public int getScrollTop() {
		return scrollTop;
	}

	@Override
	public void show(IElementModel newElementModel) {
		IElementModelDelta<IViewModel> modelElementDelta = (IElementModelDelta<IViewModel>) getModelElementDelta();

		((ViewModelDelta) modelElementDelta).showElementModel(newElementModel);
	}
}
